<?php
require_once("company.php");
require_once ("util.php");
require_once ("sql.php");

class ManageCompany {
	public  function addCompany($company,$companyShort){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::COMPANY_ADD;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('ss',$companyShort,$company);
		$status=$stmt->execute();

		$conn->commit();
		$conn->close();
		return $status;
	}
	public  function updateCompany($id,$company,$companyShort){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::COMPANY_UPDATE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('ssd',$company,$companyShort,$id);
		$status=$stmt->execute();
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function deleteCompany($id){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::COMPANY_DELETE;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$id);
		$status=$stmt->execute();
		$conn->commit();
		$conn->close();
		return $status;
	}
	public function getCompanyList($unitId=null){
		$util=new DBUtil();
		$conn=$util->getConnection();
		if($unitId==null){
			$sql = SQLQuery::COMPANY_ADMIN_LIST;
			$stmt = $conn->prepare( $sql);
		}else{
			$sql = SQLQuery::COMPANY_USER_LIST;
			$stmt = $conn->prepare( $sql);
			$stmt->bind_param('d',$unitId);
		}
		$status=$stmt->execute();
		$result = $stmt->get_result ();
		$companies=array();
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$company=new Company();
				$company->setId($row ["ID"]);
				$company->setCompany($row ["COMPANY"]);
				$company->setCompanyShort($row ["COMPANY_SHORT"]);
				array_push($companies,$company);
			}
		}
		$conn->close();
		return $companies;

	}
	public function getCompany($companyId){
		$util=new DBUtil();
		$conn=$util->getConnection();
		$sql = SQLQuery::COMPANY_FIND;
		$stmt = $conn->prepare( $sql);
		$stmt->bind_param('d',$companyId);
		$status=$stmt->execute();
		$result = $stmt->get_result ();
		$company=null;
		if ($result->num_rows > 0) {
			while ( $row = $result->fetch_array () ) {
				$company=new Company();
				$company->setId($row ["ID"]);
				$company->setCompany($row ["COMPANY"]);
				$company->setCompanyShort($row ["COMPANY_SHORT"]);
			}
		}
		$conn->close();
		return $company;
	}
}
?>